use axum::Json;
use magic_crypt::{new_magic_crypt, MagicCrypt256, MagicCrypt64, MagicCryptTrait};
use serde_json::Value;

use crate::code;
/* --- DES-64 / AES-128 / AES-192 / AES-256 加密 --------------------------------------------------------------- */
fn get_key_64() -> MagicCrypt64{ new_magic_crypt!( config::KEY, 64) }

pub fn encode_64(str:&str)-> String { get_key_64().encrypt_str_to_base64( str ) }

pub fn decode_64(encode:&str)->Result<String, Json<Value>>{
    match get_key_64().decrypt_base64_to_string(&encode) {
        Ok( f ) => Ok( f ),
        Err(e) => Err(code::sys_(e.to_string()))
    }
}
// 256
fn get_key_256() -> MagicCrypt256{ new_magic_crypt!( config::KEY, 256) }

pub fn encode_256(str:&str)-> String { get_key_256().encrypt_str_to_base64( str ) }

pub fn decode_256(encode:&str)->Result<String, Json<Value>>{
    match get_key_256().decrypt_base64_to_string(&encode) {
        Ok( f ) => Ok( f ),
        Err(e) => Err(code::sys_(e.to_string()))
    }
}